﻿$(document).ready(function () {
    "use strict";
    var iscreate = true;
    var opt = {
        controls: [
            {
                name: "usermodal",
                type: "modal",
                body: [
                    {
                        type: "gridtable",
                        name: "modalgriduser"
                    }
                ]
            },
            {
                type: "row",
                controls: [{
                    type: "header", title: "Supplier Management", breadcrumb: "mstbarang",
                    toolbarbutton: [
                                { type: "btn", name: "btnCreate", text: "create", color: "blue", icon: "plus", onClick: create },
                                { type: "btn", name: "btnEdit", text: "Edit", color: "green", icon: "edit", onClick: edit },
                                { type: "btn", name: "btnDelete", text: "Delete", color: "red", icon: "times", onClick: deleted },
                                { type: "btn", name: "btnSave", text: "Save", color: "purple", icon: "save", cls: "hide", onClick: save },
                                { type: "btn", name: "btnCancel", text: "Cancel", color: "yellow", icon: "undo", cls: "hide", onClick: cancel }
                    ]
                }
                ]
            },
        {
            type: "row",
            controls: [
                {
                    type: "portlet", name: "pnlfilter", color: "green", title: "List Material", icon: "home", hidetool: ["remove", "config"],
                    body: [
                        {
                            type: "formgroup",
                            controls: [
                                   { name: "fltrKdSupp", type: "textbox", label: "Kode Barang", cls: "col-md-3", placeholder: "Kode Barang" }

                            ]
                        },
                            {
                                type: "formgroup",
                                controls: [
                                    { name: "fltrNmSupp", type: "textbox", label: "Nama Barang", cls: "col-md-5", placeholder: "Nama barang" }
                                ]
                            },
                            {
                                type: "formgroup",
                                controls: [
                                        { name: "fltrAddr", type: "textbox", label: "Barcode", cls: "col-md-3", placeholder: "Barcode" }
                                ]
                            },
                            {
                                type: "formgroupbtn",
                                controls: [
                                        { name: "btnSearch", type: "btn", color: "blue", text: "Search", icon: "search", onClick: refreshgrid }

                                ]
                            },

                        {
                            type: "gridtable",
                            name: "gridsupplier"
                        }
                    ]

                }
            ]
        },
        {
            type: "row",
            controls: [
                {
                    type: "portlet", mustvalid: true, name: "pnlInput", color: "purple", title: "Manage Supplier", icon: "home", hidetool: ["remove", "config"], hidden: true,
                    body: [
                        {
                            type: "lefttab",
                            name: "usertab",
                            //active:2,
                            tabitem: [
                                { icon: "user", text: "Supplier Detial" },
                                { icon: "home", text: "Alamat" },
                                { icon: "phone", text: "Contact" }
                            ],
                            tabcontent: [
                                {
                                    controls: [
                                            {
                                                type: "formgroup",
                                                controls: [
                                                       {
                                                           name: "KdSupp", type: "textbox", label: "Kode Supplier", cls: "col-md-5",
                                                           placeholder: "Kode Supplier", required: true, minlength: 5,clslabel: "col-md-3"
                                                       }
                                                ]
                                            },
                                            {
                                                type: "formgroup",
                                                controls: [
                                                       {
                                                           name: "NmSupp", type: "textbox", label: "Nama Supplier", cls: "col-md-5",
                                                           placeholder: "Nama supplier", required: true, clslabel: "col-md-3"
                                                       }
                                                ]
                                            },
                                            {
                                                type: "formgroup",
                                                controls: [
                                                    {
                                                        name: "BankId", type: "dropdownlist", title: "Bank",
                                                        cls: "col-md-5",dfltText: "-- SELECT ONE --",clslabel: "col-md-3"
                                                    }
                                                ]
                                            },
                                            {
                                                type: "formgroup",
                                                controls: [
                                                       {
                                                           name: "BankAcc", type: "textbox", label: "No. Rek",
                                                           cls: "col-md-5", placeholder: "Nomor Rekening", clslabel: "col-md-3"
                                                       }
                                                ]
                                            }
                                    ]
                                },
                                {
                                    controls: [
                                            {
                                                type: "formgroup",
                                                controls: [
                                                       {
                                                           name: "Addr", type: "textarea", label: "Alamat", cls: "col-md-5",
                                                           placeholder: "Alamat",required:true
                                                       }
                                                ]
                                            },
                                            {
                                                type: "formgroup",
                                                controls: [
                                                       {
                                                           name: "Province", type: "dropdownlist", title: "Provinsi",
                                                           cls: "col-md-5", ddlcls:"mstarea",dfltText: "-- SELECT ONE --"
                                                       }
                                                ]
                                            },
                                            {
                                                type: "formgroup",
                                                controls: [
                                                       {
                                                           name: "City", type: "dropdownlist", title: "Kota/Kabupaten",
                                                           cls: "col-md-5",ddlcls:"mstarea", dfltText: "-- SELECT ONE --"
                                                       }
                                                ]
                                            },
                                            {
                                                type: "formgroup",
                                                controls: [
                                                       {
                                                           name: "SubDistrict", type: "dropdownlist", title: "Kecamatan",
                                                           cls: "col-md-5",ddlcls:"mstarea", dfltText: "-- SELECT ONE --"
                                                       }
                                                ]
                                            }
                                            ,
                                            {
                                                 type: "formgroup",
                                                 controls: [
                                                        {
                                                            name: "ZipCode", type: "textbox", label: "Kode Pos",txtcls:"numeric",
                                                            numeric:true,cls: "col-md-5", placeholder: "Nomor Rekening"
                                                        }
                                                 ]
                                             }

                                    ]
                                },
                                {
                                    controls: [
                                            {
                                                type: "formgroup",
                                                controls: [
                                                       {
                                                           name: "Email", type: "textbox", label: "Email", cls: "col-md-5",
                                                           placeholder: "Email"
                                                       }
                                                ]
                                            },
                                            {
                                                type: "formgroup",
                                                controls: [
                                                       {
                                                           name: "Phone1", type: "textbox", label: "Phone 1", cls: "col-md-5",
                                                           placeholder: "Phone 1",required:true
                                                       }
                                                ]
                                            },
                                            {
                                                type: "formgroup",
                                                controls: [
                                                       {
                                                           name: "Phone2", type: "textbox", cls: "col-md-5", placeholder: "Phone 2"
                                                           
                                                       }
                                                ]
                                            },
                                            {
                                                type: "formgroup",
                                                controls: [
                                                       {
                                                           name: "Fax", type: "lookup", label: "Fax", cls: "col-md-5",
                                                           placeholder: "Fax"
                                                       }
                                                ]
                                            }

                                    ]
                                }
                            ]
                        }   
                    ]
                }
            ]
        }
        ]
    }


    var widget = new PageEngine(opt);
    widget.render(init);
    

    function init() {
        widget.PopulateCombo({ nmspc: "bank", name: "BankId" })
        widget.PopulateCombo({ uri:  'api/APIcom/comboarea/p/', name: "Province" })        
        refreshgrid();
        //widget.showonlyportlet("pnlInput");
        //widget.showbuttontoolbar("#btnSave,#btnCancel");


    }


    function create() {
        iscreate = true;
        widget.clearcontrol("#pnlInput");
        widget.showonlyportlet("pnlInput");
        widget.showbuttontoolbar("#btnSave,#btnCancel");
        widget.readonly({ name: "#KdSupp", readonly: false });
    }

    function edit() {
        var rows = $("#gridsupplier").dataTable().fnGetNodes();
        var cb = $("#gridsupplier input:checked");
        if (cb.length == 0) {
            widget.showmessage([{ type: "error", msg: "please select data first", title: "error edit" }]);
        }
        else if (cb.length > 1) {
            widget.showmessage([{ type: "error", msg: "multiple edit is not active yet", title: "error edit" }])
        }
        else {
            EditItem(cb.val());
        }
    }
    function deleted() {
        widget.Confirm({
            title: "Delete Supplier",
            msg: "<p>Anda yakin ingin menghapus supplier?</p>",
            ok: function () {
                var rslt = widget.submit(widget.getbaseurl() + 'scp/mdm/supplier/delete',
                    { cb: JSON.stringify(widget.getSelectedGridRow("gridsupplier")) }, true);
                if (rslt.success) {
                    widget.showmessage([{ msg: "data has been deleted", title: "Done", type: "success" }]);
                }
                refreshgrid();
            }
        });

      
    }

    function save() {
 
        if (!widget.isvalid()) {
        }
        else {
            var data = $("#pnlInput").serializeObject();
            data.iscreate = (iscreate ? iscreate : 0);
            var rslt = widget.submit(widget.getbaseurl() + 'scp/mdm/supplier/save', data, true);
            if (rslt.success) {
                widget.showmessage([{ msg: "data saved successfully", title: "Done", type: "success" }]);
            }
            else {
                widget.showmessage([{ msg: "Failed to Save: " + rslt.message, title: "Error", type: "error" }]);
            }
            widget.showonlyportlet('pnlfilter');
            widget.showbuttontoolbar("#btnCreate,#btnEdit,#btnDelete");
            refreshgrid();
        }
    }

    function cancel() {
        widget.showonlyportlet("pnlfilter");
        widget.showbuttontoolbar("#btnCreate,#btnEdit,#btnDelete");
    }


    function EditItem(data) {
        iscreate = false;
        widget.clearcontrol("#pnlInput");
        widget.populatedata({ url: "api/mdm/apisupp/mstsupplier/" + data }, "#pnlInput");
        widget.showonlyportlet("pnlInput");
        widget.showbuttontoolbar("#btnSave,#btnCancel");
        widget.readonly({ name: "#KdSupp" });
    }

    function refreshgrid() {
        var data = $("#pnlfilter").serializeObject();
        widget.reloadgrid({
            url: "api/mdm/grid/supplier",
            name: "gridsupplier",
            checkbox: true,
            params: data,
            key: "KdSupp",
            filter: true,
            columns: [
            { mData: "KdSupp", sTitle: "Kode Supplier " },
            { mData: "NmSupp", sTitle: "Nama Supplier" },            
            { mData: "Phone1", sTitle: "Telp" },
            { mData: "Email", sTitle: "Email" },
            { mData: "Addr", sTitle: "Alamat" }            
            ],
            callback: function (data) {
                EditItem(data[1]);
            }
        });
    }


   

});